#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Currency Converter Copyright (C) 2009  Chris Marsh
#
# Currency Converter is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or any later version.
#
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# *********************************************************************************
#
# options_dlg.py, This module represents the gui of the options dialog.
#
# *********************************************************************************

from PyQt4.QtCore import *
from PyQt4.QtGui import *

# *********************************************************************************

class OptionState(object):
    
    def __init__(self,  autoUpdate=False,  altColor="#EEEEEE", mainColor="#FFFFFF"):
        self.__autoUpdate = autoUpdate
        self.__mainColor = QColor()
        self.__altColor = QColor()
        self.setAltColor(altColor)
        self.setMainColor(mainColor)
    
    def autoUpdate(self):
        return self.__autoUpdate
    
    def mainColor(self):
        return self.__mainColor
    
    def altColor(self):
        return self.__altColor
    
    def setAutoUpdate(self,  state):
        self.__autoUpdate = state
    
    def setMainColor(self,  color):
        if type(color) == str:
            self.__mainColor = QColor(color)
        else:
            self.__mainColor = color
    
    def setAltColor(self,  color):
        if type(color) == str:
            self.__altColor = QColor(color)
        else:
            self.__altColor = color

# *********************************************************************************
    
class OptionsDialog(QDialog):
    
    def __init__(self,  optionState=None,  parent=None):
        super(OptionsDialog,  self).__init__(parent)
        
        colorLabelStyle = QFrame.Sunken | QFrame.Panel
        
        colorBox = QGroupBox("Table Colors")
        self.mainColorLabel = QLabel("Background Color")
        self.mainColorLabel.setFrameStyle(colorLabelStyle)
        mainColorButton = QPushButton("...")
        self.altColorLabel = QLabel("Alternative Color")
        self.altColorLabel.setFrameStyle(colorLabelStyle)
        altColorButton = QPushButton("...")
        
        colorLayout = QGridLayout()
        colorLayout.addWidget(self.mainColorLabel,  0, 0)
        colorLayout.addWidget(mainColorButton, 0, 1)
        colorLayout.addWidget(self.altColorLabel, 1, 0)
        colorLayout.addWidget(altColorButton, 1, 1)
        
        colorBox.setLayout(colorLayout)
        
        updateBox = QGroupBox("Update")
        self.autoUpdateCheck = QCheckBox("Update on program start")
        
        updateLayout = QVBoxLayout()
        updateLayout.addWidget(self.autoUpdateCheck)
        updateBox.setLayout(updateLayout)
        
        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel | QDialogButtonBox.Apply)
        buttonBox.button(QDialogButtonBox.Ok).setDefault(True)
        
        dialogLayout = QVBoxLayout()
        dialogLayout.addWidget(colorBox)
        dialogLayout.addWidget(updateBox)
        dialogLayout.addWidget(buttonBox)
        
        self.connect(mainColorButton, SIGNAL("clicked()"), lambda: self.slotSetColor("mainColor"))
        self.connect(altColorButton,  SIGNAL("clicked()"),  lambda: self.slotSetColor("altColor"))
        self.connect(buttonBox,  SIGNAL("rejected()"),  self,  SLOT("reject()"))
        self.connect(buttonBox,  SIGNAL("accepted()"),  self,  SLOT("accept()"))
        self.connect(buttonBox.button(QDialogButtonBox.Apply),  SIGNAL("clicked()"), self.applyChanges)
        
        if optionState == None:
            self.optionState = OptionState()
        else:
            self.optionState = optionState
        if self.optionState.autoUpdate():
            self.autoUpdateCheck.setCheckState(Qt.Checked)
        else:
            self.autoUpdateCheck.setCheckState(Qt.Unchecked)
        self.setMainColor(QColor(self.optionState.mainColor()))
        self.setAltColor(QColor(self.optionState.altColor()))
        
        self.setLayout(dialogLayout)
        self.setWindowTitle("Options")

    def accept(self):
        self.applyChanges()
        QDialog.accept(self)
    
    def applyChanges(self):
        self.optionState.setAltColor(self.altColorLabel.palette().color(QPalette.Window))
        self.optionState.setMainColor(self.mainColorLabel.palette().color(QPalette.Window))
        self.optionState.setAutoUpdate(self.autoUpdateCheck.checkState()==Qt.Checked)
        self.emit(SIGNAL("optionsApplied"),  self.optionState)
    
    def setMainColor(self,  color):
        self.mainColorLabel.setPalette(QPalette(color))
        self.mainColorLabel.setAutoFillBackground(True)

    def setAltColor(self,  color):
        self.altColorLabel.setPalette(QPalette(color))
        self.altColorLabel.setAutoFillBackground(True)

    def slotSetColor(self,  colorSource):
        if colorSource == "mainColor":
            color = self.mainColorLabel.palette().color(QPalette.Window)
        else:
            color = self.altColorLabel.palette().color(QPalette.Window)
        color = QColorDialog.getColor(color)
        if (color.isValid()):
            if colorSource == "mainColor":
                self.setMainColor(color)
            else:
                self.setAltColor(color)

# *********************************************************************************

def main():
    import sys
    app = QApplication(sys.argv)
    optionsDlg = OptionsDialog()
    optionsDlg.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
